package com.linktco.common.utils;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * @author zhangnx
 */
@Component
public class SpringUtils implements ApplicationContextAware {

    private static ApplicationContext context = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtils.context = applicationContext;
    }

    public static <T> T getBean(Class<T> type) {
        return context.getBean(type);
    }

    public static <T> T getBean(String beanId) {
        return beanId == null ? null : (T) context.getBean(beanId);
    }




    public static boolean isDev() {
        Environment environment = getBean(Environment.class);
        String[] activeProfiles = environment.getActiveProfiles();
        List<String> activeProfileList = Arrays.asList(activeProfiles);
        return activeProfileList.contains("dev");
    }

    public static boolean isTest() {
        Environment environment = getBean(Environment.class);
        String[] activeProfiles = environment.getActiveProfiles();
        List<String> activeProfileList = Arrays.asList(activeProfiles);
        return activeProfileList.contains("test");
    }

    public static boolean isProd() {
        Environment environment = getBean(Environment.class);
        String[] activeProfiles = environment.getActiveProfiles();
        List<String> activeProfileList = Arrays.asList(activeProfiles);
        return activeProfileList.contains("prod");
    }


}
